import { createRouter, createWebHistory } from 'vue-router'
import VueCookie from 'vue-cookie';
import { ref } from 'vue';
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
	  {
		  path: '/',
		  name: 'Index',
		  component: () => import('@/views/sys/client'),
          children: [
            {
                path: '/',
                name: 'Indexmain',
                component: () => import('@/views/sys/client/index-part'),
				children: [
					{
						path: '/',
						name: 'Indexmainfooter',
						component: () => import('@/views/sys/client/index-part/about-us'),
					}
				]
            },
            {
                path: '/goods',
                name: 'GoodsIndex',
                props: true,
                component: () => import('@/views/sys/client/goods'),
				children:[
					{
						path: '/goods',
						name: 'GoodsMenu',
						props: true,
						component: () => import('@/views/sys/client/index-part/menu'),
					}
				]
            },
			{
			    path: '/goodsInfo',
			    name: 'GoodsInfo',
			    props: true,
			    component: () => import('@/views/sys/client/goods-part')
			},
			
            {
                path: '/user',
                name: 'UserIndex',
                component: () => import('@/views/sys/client/user'),
                meta: { requiresAuth: true },// 需要登录才能访问
                children: [
                    {
                        path: "/user",
                        name: "PersonalInfo",
                        component: () => import('@/views/sys/client/user-center')
                    },
                    {
                        path: "/password-change",
                        name: "PasswordChange",
                        component: () => import('@/views/sys/client/user-center/password-change')
                    },
                    {
                        path: "/my-collection",
                        name: "MyCollection",
                        component: () => import('@/views/sys/client/user-center/my-collection')
                    },
                    {
                        path: "/coupon",
                        name: "Coupon",
                        component: () => import('@/views/sys/client/user-center/coupon')
                    },
                    {
                        path: "/recharge-transfer",
                        name: "Recharge-Transfer",
                        component: () => import('@/views/sys/client/user-center/recharge-transfer')
                    },
                    {
                        path: "/consumption-record",
                        name: "ConsumptionRecord",
                        component: () => import('@/views/sys/client/user-center/consumption-record')
                    }
                ]
            },
            {
                path: '/orders',
                name: 'OrdersIndex',
                component: () => import('@/views/sys/client/orders'),
				meta: { requiresAuth: true },// 需要登录才能访问
                children: [
                    {
                        path: '/orders',
                        name: 'AllOrders',
                        component: () => import('@/views/sys/client/orders-part')
                    },
                    {
                        path: '/paid',
                        name: 'PaidOrders',
                        component: () => import('@/views/sys/client/orders-part/paid')
                    },
                    {
                        path: '/unpaid',
                        name: 'UnPaidOrders',
                        component: () => import('@/views/sys/client/orders-part/unpaid')
                    },
                    {
                        path: '/pickup',
                        name: 'PickUp',
                        component: () => import('@/views/sys/client/orders-part/pickup')
                    },
					{
                        path: '/done',
                        name: 'OrdersDone',
                        component: () => import('@/views/sys/client/orders-part/done')						
					}
                ]
            },
            {
                path: '/community',
                name: 'CommunityIndex',
				redirect: '/welcome',
				 meta: { requiresAuth: true },
                component: () => import('@/views/service'),
                children: [
					{
					    path: '/welcome',
					    name: 'Welcome',
					    component: () => import('@/views/service/welcome'),
						
					},
                    {
                        path: '/announcement',
                        name: 'Announcement',
                        component: () => import('@/views/service/message/announcement'),
						
                    },
					{
					    path: '/chat',
					    name: 'Chat',
					    component: () => import('@/views/service/message/chat'),

					},
					{
					    path: '/topic',
					    name: 'Topic',
						props: true,
					    component: () => import('@/views/service/message/chat/topic'),

					},
                    {
                        path: '/visitor',
                        name: 'Visitor',
                        component: () => import('@/views/service/security/visitor'),
                    },
                    {
                        path: '/parking',
                        name: 'Parking',
                        component: () => import('@/views/service/security/parking'),
                    },
                    {
                        path: '/maintenance',
                        name: 'Maintenance',
                        component: () => import('@/views/service/property/maintenance'),


                    },
                    {
                        path: '/wxsm',
                        name: 'wxsm',
                        component: () => import( '@/views/service/property/maintenance/wxsm')
                    },
                    {
                        path: '/complaints',
                        name: 'Complaints',
                        component: () => import('@/views/service/property/complaints'),
                    },
                    {
                        path: '/payment',
                        name: 'Payment',
                        component: () => import('@/views/service/life/payment'),
                    },
                    {
                        path: '/utilitybills',
                        name: 'Utilitybills',
                        component: () => import('@/views/service/life/utilitybills'),

                    },
                    {
                        path: '/showFee',
                        name: 'ShowFee',
                        component:()=>import('@/views/service/life/utilitybills/showFee.vue')
                    }
                ]
            },
            {
                path: '/create-orders',
                name: 'CreateOrders',
                component: () => import('@/views/sys/client/orders-part/create-orders')
            },
            {
                path: '/paid-method',
                name: 'PaidMethod',
                component: () => import('@/views/sys/client/orders-part/paid-method')
            },
            {
                path: '/pay-success',
                name: 'PaySuccess',
                component: () => import('@/views/sys/client/orders-part/pay-success')
            },
            {
                path: '/cart',
                name: 'CartIndex',
                component: () => import('@/views/sys/client/cart')
            }
        ]
	  },
	  {
		  path:'/login',
		  name: 'LoginRegister',
		  component: () => import('@/views/login')
	  },
	  {
	  		  path:'/about-us',
	  		  name: 'AboutUs',
	  		  component: () => import('@/views/sys/about-us')
	  },
	  {
	  		  path:'/contact',
	  		  name: 'Contact',
	  		  component: () => import('@/views/sys/about-us/contact')
	  },
	  {
	  		  path:'/cooperate',
	  		  name: 'Cooperate',
	  		  component: () => import('@/views/sys/about-us/cooperate')
	  },
	  {
	  		  path:'/feedback',
	  		  name: 'Feedback',
	  		  component: () => import('@/views/sys/about-us/feedback')
	  },
	  {
	  		  path:'/join',
	  		  name: 'Join',
	  		  component: () => import('@/views/sys/about-us/join')
	  }
   
  ]
})

function ToLogin(){
	router.push({ name: 'LoginRegister' });
}
export const activeMenu = ref('/');
const monitoredRoutes = ['/', '/goods', '/orders','/user','/welcome']; // 需要监视的主路由路径

router.beforeEach((to, from, next) => {
	const isLoggedIn = VueCookie.get('token'); // 从cookie中获取用户信息
    
    if (monitoredRoutes.includes(to.path)) { 
        activeMenu.value = to.fullPath;
    }
	if (to.matched.some(record => record.meta.requiresAuth)) {
	  if (!isLoggedIn) {
		ElMessageBox.confirm('您尚未登录，请先登录。', '提示', {
		  confirmButtonText: '登录',
		  cancelButtonText: '取消',
		  type: 'warning',
		}).then(() => {
		  // 用户点击登录
		  ToLogin();
		  next();
		}).catch(() => {
		  // 用户点击取消
		  next(false);
		});
	  } else {
		// 用户已登录，允许访问
		next();
	  }
	} else {
	  // 路由不需要认证，直接允许访问
	  next();
	}
});


export default router


